package com.amorcloud.pig.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.amorcloud.pig.R;
import com.amorcloud.pig.adapter.PigListAdapter;
import com.amorcloud.pig.bean.PigInfoBean;
import com.amorcloud.pig.greendao.PigInfoBeanDao;
import com.amorcloud.pig.utils.PigDaoUtils;

import java.util.List;

/**
 *  展示所有未上传的pig信息
 */
public class PigListActivity extends BaseActivity implements AdapterView.OnItemClickListener{

    ListView listView;
    List<PigInfoBean> pigListData;
    PigListAdapter adapter;

    @Override
    protected void init() {
        setContentView(R.layout.activity_pig_list);
        initCustomToolbar(this);
        initView();
        initData();
    }

    public void initView() {
        listView = findViewById(R.id.lv_pig_list);
        listView.setEmptyView(findViewById(R.id.tv_empty));
    }

    public void initData() {
        deleteUploadSuccessPigData();
        pigListData = PigDaoUtils.queryAll();
        adapter = new PigListAdapter(this, pigListData);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    private void deleteUploadSuccessPigData() {
        List<PigInfoBean> uploadSuccessPigList = PigDaoUtils.queryWhere("where IS_UPLOAD=?", "1");
        for (int i = 0; i < uploadSuccessPigList.size(); i++) {
            PigDaoUtils.delete(uploadSuccessPigList.get(i));
        }
    }

    @Override
    protected void onRestart() {
        pigListData = PigDaoUtils.queryAll();
        adapter.updateListViewData(pigListData);
        super.onRestart();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, PigInfoDetailActivity.class);
        intent.putExtra("id", pigListData.get(position).getId());
        intent.putExtra("title", "带标信息");
        startActivity(intent);
    }
}
